#!/bin/bash
# Creates a source package
# Input (optional): source directory, destination directory, version number
# Output: .bz2 package

# exit on error
set -e

echo "Creating source package..."

# clean everything
# cd ..
# qmake kisa.pro
# make --quiet distclean
# cd resources/

# set version to 1st input argument, or 0.63 as default value
VERSION=${1-"0.63"}

# set source dir to 2nd input argument, or trunk as default
SRC=${2-".."}

# set destination dir to 3rd input argument, or installs as default
DST=${3-"../../installs"}

# set different command line args
MKDIR_OPT="--verbose --parents"
CP_OPT="--verbose --preserve"
RM_OPT="--recursive --force"
LN_OPT="--verbose --symbolic"
TAR_OPT="--verbose --create --bzip2 --file"

if [[ -d $DST/kisa-$VERSION ]]; then
  rm $RM_OPT $DST/kisa-$VERSION
fi

mkdir $MKDIR_OPT $DST/kisa-$VERSION/translations
mkdir $MKDIR_OPT $DST/kisa-$VERSION/src/icons
mkdir $MKDIR_OPT $DST/kisa-$VERSION/src/lib
mkdir $MKDIR_OPT $DST/kisa-$VERSION/src/ui

cp $CP_OPT $SRC/resources/doc/README $DST/kisa-$VERSION/
cp $CP_OPT $SRC/resources/doc/COPYING $DST/kisa-$VERSION/
cp $CP_OPT $SRC/resources/doc/ChangeLog $DST/kisa-$VERSION/
cp $CP_OPT $SRC/resources/doc/FAQ $DST/kisa-$VERSION/

cp $CP_OPT $SRC/resources/translations/*.qm $DST/kisa-$VERSION/translations

cp $CP_OPT $SRC/resources/kisa.desktop $DST/kisa-$VERSION
cp $CP_OPT $SRC/resources/mappings_override.txt $DST/kisa-$VERSION

cp $CP_OPT $SRC/src/*.{cpp,h,pro,qrc} $DST/kisa-$VERSION/src/
cp $CP_OPT $SRC/src/icons/*.* $DST/kisa-$VERSION/src/icons/
cp $CP_OPT $SRC/src/lib/*.{cpp,h,pro} $DST/kisa-$VERSION/src/lib/
cp $CP_OPT $SRC/src/ui/*.ui $DST/kisa-$VERSION/src/ui

cp $CP_OPT $SRC/kisa.pro $DST/kisa-$VERSION/

# tar it up!
cd $DST
FILENAME=kisa-$VERSION.tar.bz2
tar $TAR_OPT=$FILENAME kisa-$VERSION/

# clean-up
rm $RM_OPT kisa-$VERSION

echo "Done"
echo "Package located at" $(pwd -P)/$FILENAME
exit 0
